package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.mine.contract.RecommenderContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.ConsumerInfo;
import com.meitianhui.convenitenbuy.module.mine.presenter.RecommenderPresenter;
import com.meitianhui.convenitenbuy.util.PreferenceUtils;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.SystemUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.SettingStoreDialog;

import javax.inject.Inject;

/**
 * Created by amy on 2017/11/16.
 */

public class RecommenderActivity extends BaseToolbarActivity implements RecommenderContract.View {
    private EditText mEtRecommenderNumber;
    private Button mBtnRecommenderConfirm;
    private String mRecommenderNumber;
    private SettingStoreDialog mConfirmDialog;
    @Inject
    RecommenderPresenter mRecommenderPresenter;


    @Override
    protected int setView() {
        return R.layout.activity_me_recommender;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("推荐人信息");
        initId();
        DaggerMineComponent.builder().mineModule(new MineModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);
    }

    private void initId() {
        mEtRecommenderNumber = getViewById(R.id.et_recommender_number);
        mBtnRecommenderConfirm = getViewById(R.id.btn_recommender_confirm);
        //防手抖
        RxUtils.bindEvents(mBtnRecommenderConfirm, this);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_recommender_confirm:
                mRecommenderNumber = mEtRecommenderNumber.getText().toString().trim();
                checkNumber();
                break;
        }
    }

    private void checkNumber() {
        if (TextUtils.isEmpty(mRecommenderNumber)) {
            TipUtils.showToast(App.getInstance(), "请输入手机号", 3);
            return;
        }
        if (!StringUtils.isPhone(mRecommenderNumber)) {
            TipUtils.showToast(App.getInstance(), "请输入正确的手机号码", 3);
            return;
        }
        //隐藏软键盘
        SystemUtils.hidKeyBoard(this);

        //显示提示对话框
        mConfirmDialog = new SettingStoreDialog(this, R.style.dialog_with_alpha, new SettingStoreDialog.AlertDialogListener() {
            @Override
            public void onConfirmClick() {
                mConfirmDialog.dismiss();
                //提交至服务器
                mRecommenderPresenter.upLoadRecommender(mRecommenderNumber);
            }

            @Override
            public void onCancelClick() {
                mConfirmDialog.dismiss();
            }
        });
        mConfirmDialog.show();
        mConfirmDialog.showCancelBtn(true);
        mConfirmDialog.setDialogTitle("提示");
        mConfirmDialog.setDialogContent("您绑定的推荐人为：");
        mConfirmDialog.setDialogContentLine2(mRecommenderNumber);
        mConfirmDialog.setDialogContentLine3("一旦提交无法修改,是否确认提交?");
        mConfirmDialog.setConfirmBtnText("确定");
        mConfirmDialog.setCancelBtnText("取消");


    }

    @Override
    protected void onRefreshData() {

    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void bindRecommenderSuccess(String recommenderNumber) {
        //跟新本地的consumer对象中的推荐人的信息
        ConsumerInfo consumerInfo = InfoProvider.getConsumerInfo(this);
        consumerInfo.setReference_mobile(recommenderNumber);
        PreferenceUtils.saveObject(this, Constants.CONSUMER_INFO,consumerInfo);
        finish();
    }
}
